package com.example.xiaomaotv;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.util.ArrayList;
import java.util.List;

public class MainFragment extends Fragment implements View.OnClickListener{
    private  SyFragment syFragment;
    private JjFragment jjFragment;
    private DyFragment dyFragment;
    private ZyFragment zyFragment;
    private  DjFragment djFragment;
    private  DmFragment dmFragment;
    private View mainView;
    private List<TextView> tabList = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mainView = inflater.inflate(R.layout.main_fragment,null);
        syFragment = new SyFragment();
        jjFragment = new JjFragment();
        dyFragment = new DyFragment();
        zyFragment = new ZyFragment();
        djFragment = new DjFragment();
        dmFragment = new DmFragment();
        initView();
        selectTab(R.id.sy_text);
        changeFragment(syFragment);
        return mainView;
    }
    @Override
    public void onClick(View v) {
        int clickTabId = v.getId();
        selectTab(clickTabId);
        if (clickTabId == R.id.sy_text){
            changeFragment(syFragment);
        }else if (clickTabId == R.id.jj_text) {
            changeFragment(jjFragment);
        }else if (clickTabId == R.id.dy_text) {
            changeFragment(dyFragment);
        }else if (clickTabId == R.id.zy_text) {
            changeFragment(zyFragment);
        }else if (clickTabId == R.id.dj_text) {
            changeFragment(djFragment);
        }else {
            changeFragment(dmFragment);
        }
    }
    private void initView(){
        TextView syText = mainView.findViewById(R.id.sy_text);
        TextView jjText = mainView.findViewById(R.id.jj_text);
        TextView dyText = mainView.findViewById(R.id.dy_text);
        TextView zyText = mainView.findViewById(R.id.zy_text);
        TextView djText = mainView.findViewById(R.id.dj_text);
        TextView dmText = mainView.findViewById(R.id.dm_text);
        syText.setOnClickListener(this);
        jjText.setOnClickListener(this);
        dyText.setOnClickListener(this);
        zyText.setOnClickListener(this);
        djText.setOnClickListener(this);
        dmText.setOnClickListener(this);
        tabList.add(syText);
        tabList.add(jjText);
        tabList.add(dyText);
        tabList.add(zyText);
        tabList.add(djText);
        tabList.add(dmText);
    }
    private void selectTab(int clickedTabId) {
        // 重置所有标签的颜色
        for (TextView tab : tabList) {
            if(tab.getId() == clickedTabId){
                tab.setTextColor(getResources().getColor(R.color.checked));
            }else{
                tab.setTextColor(getResources().getColor(R.color.unchecked));
            }

        }
    }
    public void changeFragment(Fragment fragment) {
        FragmentManager manager = getParentFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.frame, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}
